package cn.panshi.mongocdc;

import java.util.Date;

import org.bson.Document;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.util.Pair;

import cn.panshi.mongocdc.CdcTtlTaskDoc.CdcTtlTaskTrigger;

@SuppressWarnings("rawtypes")
public class MongoCdcTtlTaskUtil {
	private  MongoTemplate mongoTemplate;
	
	public MongoCdcTtlTaskUtil(MongoTemplate mongoTemplate) {
		super();
		this.mongoTemplate = mongoTemplate;
	}

	public CdcTtlTaskDoc newTask(String name, Enum type, Document taskBizData, Date scheduleTime) {
		Pair<CdcTtlTaskTrigger, CdcTtlTaskDoc> taskPair = CdcTtlTaskDoc.prepareNewTask(name, type, taskBizData, scheduleTime);
		
		mongoTemplate.save(taskPair.getFirst(), CdcTtlTaskDoc.TABLE);
		mongoTemplate.save(taskPair.getSecond(), CdcTtlTaskDoc.TABLE);
		
		return taskPair.getSecond();
	}
	
	public CdcTtlTaskDoc newTask(String name, Enum type, Document taskBizData, int scheduleSecondsLater) {
		Pair<CdcTtlTaskTrigger, CdcTtlTaskDoc> taskPair = CdcTtlTaskDoc.prepareNewTask(name, type, taskBizData, scheduleSecondsLater);
		
		mongoTemplate.save(taskPair.getFirst(), CdcTtlTaskDoc.TABLE);
		mongoTemplate.save(taskPair.getSecond(), CdcTtlTaskDoc.TABLE);
		
		return taskPair.getSecond();
	}
}